// packageC/redPacket/get_red_record/get_red_record.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    urlStr: '',
    language: '',
    page:1,
    tag:'old',
    receive_log: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if(options.tag){
      this.setData({
        tag : options.tag
      });
    }
    let pageTitle = '';
    let urlStr = '';
    if(this.data.tag == 'new'){
      pageTitle = "红包领取记录";
      urlStr = app.getNetAddresss("plugin.redpack-tool.frontend.receiveLog.getList");
    }else if (this.data.tag == 'old'){
      pageTitle = "红包领取记录";
      urlStr = app.getNetAddresss("plugin.red-packet.api.logs.receiveLogs");
    } else {
      pageTitle = "额外奖励记录";
      urlStr = app.getNetAddresss("plugin.red-packet.api.red-packet-logs.getBonusReceiveLogs");
    }
    this.setData({urlStr});
    wx.setNavigationBarTitle({
      title: pageTitle
    });
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },



  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (that.data.current_page < that.data.last_page) {
      wx.showLoading({
        title: '加载中',
        mask: true,
      });
      that.setData({
        page: that.data.page + 1
      });
      that.getNext(this.data.selected);
    } else {
      wx.showToast({
        title: '没有更多',
        icon: 'none'
      });
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  getData() {
    let that = this;
    app._getNetWork({
      url: that.data.urlStr,
      data:{
        page:that.data.page
      },
      success: function(resdata) {
        var res = resdata.data;
        if(that.data.tag == 'new'){
          that.setData({
            receive_log: res.data.logs.data,
            member: res.data.member,
            amount_sum: res.data.receive_total,
            current_page: res.data.logs.current_page,
            last_page: res.data.logs.last_page
          });
        }else if (that.data.tag == 'old'){
          that.setData({
            receive_log: res.data.receive_log.list.data,
            member: res.data.member,
            amount_sum: res.data.receive_log.amount_sum,
            current_page: res.data.receive_log.list.current_page,
            last_page: res.data.receive_log.list.last_page
          });
        } else {
          that.setData({
            receive_log: res.data.receive_log.data,
            member: res.data.member,
            amount_sum: res.data.bonus_sum,
            current_page: res.data.receive_log.current_page,
            last_page: res.data.receive_log.last_page
          });
        }
        
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getNext(){
    let that = this;
    app._getNetWork({
      url: that.data.urlStr,
      data: {
        page: that.data.page
      },
      success: function (resdata) {
        var res = resdata.data;
        if(that.data.tag == 'new'){
          if (res.data.logs.current_page > res.data.logs.last_page) {
            wx.hideLoading();
            wx.showToast({
              title: '没有更多',
              icon: 'none'
            });
            that.setData({
              current_page: res.data.logs.current_page,
              last_page: res.data.logs.last_page
            });
            return;
          }else{
            wx.hideLoading();
            console.log(that.data.receive_log);
            let arr = that.data.receive_log.concat(res.data.logs.data);
            that.setData({
              receive_log: arr,
              current_page: res.data.logs.current_page,
              last_page: res.data.logs.datalast_page
            });
          }
        }else if (that.data.tag == 'old'){
          if (res.data.receive_log.list.current_page > res.data.receive_log.list.last_page) {
            wx.hideLoading();
            wx.showToast({
              title: '没有更多',
              icon: 'none'
            });
            that.setData({
              current_page: res.data.receive_log.list.current_page,
              last_page: res.data.receive_log.list.last_page
            });
            return;
          }else{
            wx.hideLoading();
            that.setData({
              receive_log: that.data.receive_log.concat(res.data.receive_log.list.data),
              member: res.data.member,
              amount_sum: res.data.receive_log.amount_sum,
              current_page: res.data.receive_log.list.current_page,
              last_page: res.data.receive_log.list.datalast_page
            });
          }
        } else {
          if (res.data.receive_log.current_page > res.data.receive_log.last_page) {
            wx.hideLoading();
            wx.showToast({
              title: '没有更多',
              icon: 'none'
            });
            that.setData({
              current_page: res.data.receive_log.current_page,
              last_page: res.data.receive_log.last_page
            });
            return;
          }else{
            wx.hideLoading();
            that.setData({
              receive_log: that.data.receive_log.concat(res.data.receive_log.data),
              member: res.data.member,
              amount_sum: res.data.bonus_sum,
              current_page: res.data.receive_log.current_page,
              last_page: res.data.receive_log.last_page
            });
          }
        }
        
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
});